package server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import smash_tris.*;

/**
 * Klasa reprezentuje połączenie pomiędzy klientem a serwerem.
 * 
 * @author Maciej Graszek
 * @author Pawel Rychly
 * 
 */
public class Connection implements Runnable {

	private SmashTrisManager manager = null;
	private Socket clientSocket = null;
	private ObjectInputStream in = null;
	private ObjectOutputStream out = null;

	public Connection(Socket clientSocket, SmashTrisManager manager) {
		// this.setClientSocket(clientSocket);
		this.clientSocket = clientSocket;
		this.manager = manager;
		System.out.println("1");
		Thread t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {
			
		try {
			System.out.println("Before...");
		//	this.in = new ObjectInputStream( new BufferedInputStream(
		//			clientSocket.getInputStream()));
		//	System.out.println("After...");
			
		//	this.in = new ObjectInputStream(clientSocket.getInputStream());
			System.out.println("2");

			this.out = new ObjectOutputStream(new BufferedOutputStream(
					clientSocket.getOutputStream()));
			this.out.flush();
			
			
			this.in = new ObjectInputStream( new BufferedInputStream(
								clientSocket.getInputStream()));
			
			while (true){
				this.out.writeObject(new DataPacket());
				this.out.flush();
				System.out.println("wysłano pakiet");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e2) {
			System.out.println("Exception");
			e2.printStackTrace();
		}
		System.out.println("3");

	}



}
